package io.finer.erp.jeecg.bas.util;

import java.util.Arrays;

public class CloseUtils {
	
	public static final int SUM = 108;
	 
    public static void main(String[] args) {
        int[] a = {21, 24, 22, 24, 26, 27, 23, 24, 29, 24, 50,130};
        // 1、数组排序
        Arrays.sort(a);
        // 2、除去数组中大于SUM的值
        int[] arr = dropGreater(a, SUM);
        // System.out.println(Arrays.toString(arr));
        // 3、求数组中排列组合的最大值
        int max = arr[arr.length-1];
        int sum = 0;
        max = getMax(arr, 0, sum, max);
        // System.out.println(max);
        // 4、求数组中符合最大值的排列组合
        int[] record = new int[arr.length];
        int[] count = {1};
        f1(0,count,0,arr,record);
    }
 
     
    // 丢掉数组中比SUM大的数
    public static int[] dropGreater(int[] arr, int num) {
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > num) {
                index = i;
                break;
            }
        }
        int[] a = new int[index];
        for (int i = 0; i < index; i++) {
            a[i] = arr[i];
        }
        return a;
    }
 
    // index表示从arr数组对应的坐标开始排列组合
    public static int getMax(int[] arr, int index, int sum, int max) {
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
            if (sum==SUM)
                return sum;
            if (sum>SUM)
                break;
            if (sum > max && sum < SUM)
                max = sum;
            return getMax(arr, i + 1, sum, max);
        }
        return max;
    }
 
 
    // count数组用来记录当前为几层循环
    public static void f1(int index,int[] count,int sum,int[] arr,int[] record) {
 
        for (int i = index; i < arr.length; i++) {
            record[count[0]-1]=arr[i];
            if (sum+arr[i]>SUM) {
                count[0]--;
                break;
            }
            if (sum+arr[i]==SUM) {
                for (int j = 0; j < count[0]; j++) {
                    System.out.printf("%d ", record[j]);
                }
                System.out.printf("\n");
                count[0]--;
                break;
            }
            if (sum+arr[i]<SUM) {
                count[0]++;
                f1(i+1,count,sum+arr[i],arr,record);
            }
        }
    }
 
 
    // 帅选出arr数组中能拼凑出来的小于max的所有数，并用数组记录组合的数字，封装sum和数组进Map集合

}
